<?php


class EmsElementTable extends Doctrine_Table
{
    
    public static function getInstance()
    {
        return Doctrine_Core::getTable('EmsElement');
    }

    public function addEnabledElementsQuery(Doctrine_Query $q = null)
    {
        if(is_null($q))
        {
            $q = Doctrine_Query::create()
                ->from('EmsElement e');
        }

        $alias = $q->getRootAlias();

        $q->andWhere($alias . '.is_enabled = ?', 1);

        return $q;
    }

    public function addPublishedElementsQuery(Doctrine_Query $q = null)
    {
        if(is_null($q))
        {
            $q = Doctrine_Query::create()
                ->from('EmsElement e');
        }

        $alias = $q->getRootAlias();

        $q->andWhere($alias. '.published = ?', 1);

        return $q;
    }

    public function addElementsBySectionIdQuery($section_id, Doctrine_Query $q = null)
    {
        if(is_null($q))
        {
            $q = Doctrine_Query::create()
                ->from('EmsElement e');
        }

        $alias = $q->getRootAlias();

        $q->andWhere($alias . '.section_id = ?', $section_id);

        return $q;
    }

    public function getEnabledAndPublishedElementsBySectionId($section_id) {
        return $this->addElementsBySectionIdQuery($section_id,
                    $this->addPublishedElementsQuery(
                        $this->addEnabledElementsQuery()))->execute();
    }
}